#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#include <QtCore>

#ifdef CAP_SCREEN_ON
#include <QSystemScreenSaver>
QTM_USE_NAMESPACE
#endif

#include "controls/ftcontrols.h"

#include "modifiable.h"

#define CONFIGURATION_VERSION 1.0

class Configuration : public QObject, public Modifiable
{
    Q_OBJECT
public:
    enum tracking_mode { automatic_trackmode, time_trackmode, distance_trackmode };
    enum coordinate_format { decimal, degrees_minutes_seconds, degrees_minutes, utm };
    explicit Configuration();
    ~Configuration();

    // Save configuration
    void SaveConfig();

    // Hardware dependant (not saved or stored but recalculated on each startup)
    bool compassAvailable();

    // RunTime data
    QSize screenSize() const;
    QSize defaultScreenSize() const;

    // General options!
    bool savePosition() const;
    void setSavePosition(bool);
    void initialPosition(double& lat, double& lon, int& alt) const;
    void setInitialPosition(double, double, int);
    bool showAds() const;
    void setShowAds(bool);
    bool internetAuthorization() const;
    void setInternetAuthorization(bool);
    bool internetAuthorized();
    void setInternetAuthorized(bool);
    bool screenAlwaysOn() const;
    void setScreenAlwaysOn(bool);
    int altitudeCorrection() const;
    void setAltitudeCorrection(int);
    QString storageDrive();
    void setStorageDrive(const QString&);
    bool gpsAutostart() const;
    void setGpsAutostart(bool);
    Configuration::coordinate_format defaultCoordFormat() const;
    void setDefaultCoordFormat( Configuration::coordinate_format );
    bool useSatellitesOnly() const;
    void setUseSatellitesOnly(bool);

    // Tracking mode stuff
    tracking_mode trackingMode(int&) const;
    tracking_mode trackingMode() const;
    int trackingTime() const;
    void setTrackingTime(int);
    int trackingDistance() const;
    void setTrackingDistance(int);
    void setTrackingMode(tracking_mode );
    void setTrackingMode(tracking_mode, int );
    bool livetrackAutosave() const;
    void setLivetrackAutosave(bool);
    bool calculateSpeed() const;
    void setCalculateSpeed(bool);
    int minimumDop() const;
    void setMinimumDop(int);

    // Breadcrumbs stuff
    bool breadcrumbsToken(QByteArray& token, QByteArray& tokenSecret) const;
    void setBreadcrumbsToken(const QByteArray& token, const QByteArray& tokenSecret);
    bool breadcrumbsAuthorized() const;
    void setBreadcrumbsAuthorized(bool);

    // Waypoints options
    bool drawWaypoint() const;
    void setDrawWaypoint(bool);
    bool drawWaypointIcon() const;
    void setDrawWaypointIcon(bool);
    bool drawWaypointName() const;
    void setDrawWaypointName(bool);
    int navigationProximity() const;
    void setNavigationProximity(int);

    // Languages
    QString selectedLanguage(QString& long_lang) const;
    QString selectedLanguage() const;
    void setSelectedLanguage(const QString&);
    QHash<QString, QString> languages() const;

    // Maps options
    bool showMapsOnStartup() const;
    void setShowMapsOnStartup(bool);

    // Get default path
    QString defaultPath();

signals:
    // A few settings also emit signals. This is to avoid polling elsewhere
    void livetrackAutosaveChanged(bool);
    void waypointsDrawChanged();
    void altitudeCorrectionChanged(int);
    void trackingModeChanged( Configuration::tracking_mode, int );
    void coordFormatChanged();
    void navigationProximityChanged(int);
    void minimumDopChanged(int);
    void showAdsChanged(bool);

private:
    // These are for things that require some more code rather than just setting a value
    void initialize_screen_always_on();
    void initialize_language();
    void initialize_screen_size();

    QSize _screenSize;
    QSize _defaultScreenSize;

    QHash<QString, QString> _languages;
    QString _selectedLanguage;
    tracking_mode _trackingMode;
    int _trackingSeconds;
    int _trackingMeters;
    bool _livetrackAutosave;
    bool _calculateSpeed;
    QByteArray _breadcrumbsToken;
    QByteArray _breadcrumbsTokenSecret;
    bool _breadcrumbsAuthorized;
    bool _drawWaypoint;
    bool _drawWaypointIcon;
    bool _drawWaypointName;
    QString _storageDrive;
    bool _savePosition;
    double _initialLongitude;
    double _initialLatitude;
    bool _gpsAutostart;
    int _initialAltitude;
    bool _internetAuthorization;
    bool _internetAuthorized;
    bool internet_explicitly_granted;
    bool _screenAlwaysOn;
    int _altitudeCorrection;
    int _navigationProximity;
    bool _showAds;
    int _minimumDop;

    bool _showMapsOnStartup;
    bool _useSatellitesOnly;

#ifdef CAP_SCREEN_ON
    QSystemScreenSaver* screen_saver;
#endif

    QString config_filename;

    bool _compassAvailable;
    QString _defaultPath;
    QTranslator translator;

    Configuration::coordinate_format _defaultCoordFormat;
};

extern Configuration* configuration;

#endif // CONFIGURATION_H
